package com.party.admin.biz.mall;

import cn.jpush.api.utils.StringUtils;
import com.party.admin.biz.distribution.DistributionBizService;
import com.party.admin.utils.RealmUtils;
import com.party.admin.web.dto.input.common.CommonQuery;
import com.party.common.paging.Page;
import com.party.core.model.mall.MallShowCategory;
import com.party.core.model.mall.MallShowCategoryQuery;
import com.party.core.model.mall.MallShowGoods;
import com.party.core.service.mall.IMallShowCategoryService;
import com.party.core.service.mall.IMallShowGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 展示商品类型服务接口
 * Created by cyl
 *
 * @date 2017/8/26
 * @time 16:08
 */
@Service
public class MallShowCategoryBizService {
    @Autowired
    private IMallShowCategoryService mallShowCategoryService;
    @Autowired
    private MallGoodsBizService mallGoodsBizService;
    @Autowired
    private DistributionBizService distributionBizService;
    @Autowired
    private IMallShowGoodsService mallShowGoodsService;

    /**
     * 商品列表数据
     *
     * @param mallShowCategoryQuery 商品类型
     * @param page                  分页参数
     * @return 列表数据
     */
    public List<MallShowCategory> list(MallShowCategoryQuery mallShowCategoryQuery, Page page) {
        if (null != mallShowCategoryQuery.getTimeType() && !new Integer(0).equals(mallShowCategoryQuery.getTimeType())) {
            CommonQuery commonQuery = new CommonQuery();
            commonQuery.setTimeType(mallShowCategoryQuery.getTimeType());
            mallShowCategoryQuery.setCreateStart(commonQuery.getCreateStart());
            mallShowCategoryQuery.setCreateEnd(commonQuery.getCreateEnd());
        }
        return mallShowCategoryService.queryListPage(mallShowCategoryQuery, page);
    }

    /**
     * 将类型(包括下级商品)复制到目标主题去
     *
     * @param sourceId     源id 专题/模板
     * @param subjectId    新的主题id
     * @param fromTemplate 来自模板
     * @param orgId
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void copyCategoryBiz(String sourceId, String subjectId, boolean fromTemplate, String orgId) {
        String createBy = RealmUtils.getCurrentUser().getId();
        MallShowCategoryQuery mallShowCategoryQuery = new MallShowCategoryQuery();
        if (fromTemplate) {
            mallShowCategoryQuery.setTemplateId(sourceId);
        } else {
            mallShowCategoryQuery.setSubjectId(sourceId);
        }
        List<MallShowCategory> mallShowCategories = mallShowCategoryService.list(mallShowCategoryQuery);
        for (MallShowCategory showCategory : mallShowCategories) {
            String sourceCategoryId = showCategory.getId();
            showCategory.setId(null);
            showCategory.setCreateDate(null);
            showCategory.setUpdateDate(null);
            showCategory.setCreateBy(createBy);
            showCategory.setUpdateBy(createBy);
            showCategory.setSubjectId(subjectId);
            if (fromTemplate) {
                showCategory.setTemplateId(null);
            }
            String newCategoryId = mallShowCategoryService.insert(showCategory);
            if (StringUtils.isNotEmpty(newCategoryId)) {
                /*修改展示商品*/
                List<MallShowGoods> mallShowGoods = mallShowGoodsService.findByCategoryId(sourceCategoryId);
                for (MallShowGoods showGoods : mallShowGoods) {
                    showGoods.setId(null);
                    showGoods.setUpdateDate(null);
                    showGoods.setCreateDate(null);
                    showGoods.setCreateBy(createBy);
                    showGoods.setUpdateBy(createBy);
                    showGoods.setCategoryId(newCategoryId);
                    showGoods.setSubjectId(sourceId);
                    if (fromTemplate) {
                        showGoods.setTemplateId(null);
                    }
                    mallShowGoodsService.insert(showGoods);

                    // 保存分销关系
                    if (null != showGoods.getType() && StringUtils.isNotEmpty(showGoods.getTargetId()) && StringUtils.isNotEmpty(orgId)) {
                        List<String> goodsIds = mallGoodsBizService.getGoodsIds(showGoods.getType(), showGoods.getTargetId());
                        distributionBizService.saveShowGoodsDistributionBiz(orgId, goodsIds);
                    }
                }
            }
        }
    }
}
